package com.begye.smartloan.apptest.ext

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Typeface
import android.os.Handler
import android.os.Looper
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.util.TypedValue
import android.view.View
import android.widget.TextView
import java.util.Timer
import java.util.TimerTask

fun TextView.addColor(words: String? = null, hexColor: String) {
    val text = this.text.toString()
    var start = -1
    var end = text.length

    words?.let {
        if (it.isNotBlank()) {
            start = text.indexOf(it)
            if (start >= 0) {
                end = start + it.length
            }
        }
    }

    if (start >= 0) {
        val spanString = SpannableString(text)
        spanString.setSpan(
            ForegroundColorSpan(Color.parseColor(hexColor)),
            start,
            end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        this.text = spanString
    }
}

@SuppressLint("SetTextI18n")
fun TextView.addCountdown(
    seconds: Int = 60,
    autoDismiss: Boolean = true,
    unableResource: Int? = null,
    onTick: (Int) -> Unit,
    onFinished: () -> Unit
) {
    val curResource = background
    val curText = text
    var curTick = seconds
    val timer = Timer()
    val looper = Handler(Looper.getMainLooper())
    timer.schedule(object : TimerTask() {
        override fun run() {
            looper.post {
                isEnabled = false
                text = "${curTick}s"
                unableResource?.let {
                    setBackgroundResource(it)
                }
                onTick.invoke(curTick)
                if (curTick == 0) {
                    timer.cancel()
                    background = curResource
                    text = curText
                    isEnabled = true

                    visibility = if (autoDismiss) {
                        View.INVISIBLE
                    } else {
                        View.VISIBLE
                    }
                    onFinished.invoke()
                }
                curTick -= 1
            }
        }
    }, 0, 1000)
}


fun TextView.addUnderLine(words: String? = null, hexColor: String? = null) {
    val text = this.text.toString()
    var start = 0
    var end = text.length

    words?.let {
        if (it.isNotBlank()) {
            start = text.indexOf(it)
            if (start >= 0) {
                end = start + it.length
            }
        }
    }

    val spanString = SpannableString(text)
    spanString.setSpan(
        UnderlineSpan(),
        start,
        end,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )

    hexColor?.let {
        spanString.setSpan(
            ForegroundColorSpan(Color.parseColor(it)),
            start,
            end,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    }

    this.text = spanString
}

fun TextView.addBold() {
    this.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
}

fun TextView.addNormal() {
    this.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
}

fun TextView.addFontSize(size: Float) {
    this.setTextSize(TypedValue.COMPLEX_UNIT_SP, size)
}

fun TextView.addTextColor(hexColor: String) {
    try {
        this.setTextColor(Color.parseColor(hexColor))
    } catch (e: Exception) {
        //
    }
}
